package y2017.m10.d11;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Md5 {

    private static String getDate() {
        String result = null;
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        SimpleDateFormat MM = new SimpleDateFormat("MM");
        SimpleDateFormat dd = new SimpleDateFormat("dd");
        SimpleDateFormat HH = new SimpleDateFormat("HH");
        SimpleDateFormat mm = new SimpleDateFormat("mm");

        yyyy.setTimeZone(TimeZone.getTimeZone("UTC"));
        MM.setTimeZone(TimeZone.getTimeZone("UTC"));
        dd.setTimeZone(TimeZone.getTimeZone("UTC"));
        HH.setTimeZone(TimeZone.getTimeZone("UTC"));
        mm.setTimeZone(TimeZone.getTimeZone("UTC"));

        result = yyyy.format(new Date())
                + "1"
                + MM.format(new Date())
                + "2"
                + dd.format(new Date())
                + "3"
                + HH.format(new Date())
                + "4"
                + (Integer.parseInt(mm.format(new Date())));
        System.out.println(result);
        return result;
    }

    private static String generate(String md5) {
        String result = null;
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte anArray : array) {
                sb.append(Integer.toHexString((anArray & 0xFF) | 0x100), 1, 3);
            }
            result = sb.toString();
        } catch (java.security.NoSuchAlgorithmException ignored) {
            System.out.println(ignored.getMessage());
        }
        return result;
    }

    public static void main(String[] args) {
        String md5 = getDate();
        md5 = generate(md5);
        System.out.println(md5);
        System.out.println(md5.substring(1, 5));

//        81dc9bdb52d04dc20036dbd8313ed055
//        System.out.println(generate("1234"));
//        System.out.println(generate("12345"));

    }
}
